﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.DAL;

namespace KPIS.GERP.GBMS.DAL
{
    [Serializable]
    public sealed class StrategyTypeDAL : IStrategyTypeDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public int Insert(StrategyTypeInfo strategyTypeInfo)
        {
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@code", strategyTypeInfo.Code);
                dbManager.AddParameters(1, "@name", strategyTypeInfo.Name);
                dbManager.AddParameters(2, "@descr", strategyTypeInfo.Description);
                dbManager.AddParameters(3, "@root", strategyTypeInfo.IsRoot);
                dbManager.AddParameters(4, "@status", SystemInfo._ActiveRecordStatus);

                return dbManager.ExecuteNonQuery(CommandType.Text, "INSERT INTO BGM_MAS_STRATEGY_TYPE(BGM_STRAT_TYPE_CODE, BGM_STRAT_TYPE_NAME, BGM_STRAT_TYPE_DESCR, ISROOT, RECORD_STATUS) VALUES (@code, @name, @descr, @display, @status)");
            }
            catch
            {
                return 0;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public int Update(StrategyTypeInfo strategyTypeInfo)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@seq", strategyTypeInfo.Sequence);
                dbManager.AddParameters(1, "@code", strategyTypeInfo.Code);
                dbManager.AddParameters(2, "@name", strategyTypeInfo.Name);
                dbManager.AddParameters(3, "@descr", strategyTypeInfo.Description);
                dbManager.AddParameters(4, "@root", strategyTypeInfo.IsRoot);
                dbManager.AddParameters(5, "@status", SystemInfo._ActiveRecordStatus);

                strSQL = "UPDATE BGM_MAS_STRATEGY_TYPE"
                + " SET BGM_STRAT_TYPE_CODE = @code"
                    + ", BGM_STRAT_TYPE_NAME = @name"
                    + ", BGM_STRAT_TYPE_DESCR = @descr"
                    + ", ISROOT = @root"
                    + ", RECORD_STATUS = @status"
                + " WHERE BGM_STRAT_TYPE_SEQ = @seq";

                return dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                return 0;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public int Delete(int seq)
        {
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@seq", seq);
                dbManager.AddParameters(1, "@status", SystemInfo._DeleteRecordStatus);

                return dbManager.ExecuteNonQuery(CommandType.Text, "UPDATE BGM_MAS_STRATEGY_TYPE SET RECORD_STATUS = @status WHERE BGM_STRAT_TYPE_SEQ = @seq");
            }
            catch
            {
                return 0;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public IList<StrategyTypeInfo> Search(StrategyTypeInfo strategyTypeInfo)
        {
            List<StrategyTypeInfo> stratType = new List<StrategyTypeInfo>();
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@seq", strategyTypeInfo.Sequence);
                dbManager.AddParameters(1, "@code", strategyTypeInfo.Code);
                dbManager.AddParameters(2, "@name", strategyTypeInfo.Name);
                dbManager.AddParameters(3, "@descr", strategyTypeInfo.Description);
                dbManager.AddParameters(4, "@root", strategyTypeInfo.IsRoot);
                dbManager.AddParameters(5, "@status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT BGM_STRAT_TYPE_SEQ, BGM_STRAT_TYPE_CODE, BGM_STRAT_TYPE_NAME, BGM_STRAT_TYPE_DESCR, RECORD_STATUS, IS_ROOT"
                    + " FROM BGM_MAS_STRATEGY_TYPE"
                    + " WHERE RECORD_STATUS not like @status"
                        + ((strategyTypeInfo.Sequence > 0) ? " AND BGM_STRAT_TYPE_SEQ = @seq" : "")
                        + ((strategyTypeInfo.Code.Length > 0) ? " AND BGM_STRAT_TYPE_CODE = @code" : "")
                        + ((strategyTypeInfo.Name.Length > 0) ? " AND BGM_STRAT_TYPE_NAME = @name" : "")
                        + ((strategyTypeInfo.IsRoot > 0) ? " AND IS_ROOT = @root" : "")
                        + ((strategyTypeInfo.Description.Length > 0) ? " AND BGM_STRAT_TYPE_DESCR = @descr" : "");

                DataTable dtb = dbManager.ExecuteDataTable(CommandType.Text, strSQL);

                foreach (DataRow dr in dtb.Rows)
                {
                    StrategyTypeInfo stratRecord = new StrategyTypeInfo();

                    stratRecord.Sequence = Convert.ToInt32(dr["BGM_STRAT_TYPE_SEQ"]);
                    stratRecord.Code = dr["BGM_STRAT_TYPE_CODE"].ToString();
                    stratRecord.Name = dr["BGM_STRAT_TYPE_NAME"].ToString();
                    stratRecord.Description = dr["BGM_STRAT_TYPE_DESCR"].ToString();
                    stratRecord.Status = dr["RECORD_STATUS"].ToString();
                    stratRecord.IsRoot = Convert.ToInt32(dr["IS_ROOT"]);

                    stratType.Add(stratRecord);
                }

                //while (rdr.Read())
                //{
                //    StrategyTypeInfo stratRecord = new StrategyTypeInfo(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.IsDBNull(3) ? "" : rdr.GetString(3), rdr.GetString(4), rdr.GetInt32(5), null);
                //    stratType.Add(stratRecord);
                //}

                return stratType;
            }
            catch (Exception ex)
            {
                return null;
                throw ex;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
    }
}